﻿@{
    Layout = "~/Views/Shared/_Style.cshtml";
}
<link href="~/js/lib/layui/dist/css/layui.css" rel="stylesheet" />
<script src="~/js/lib/jquery/jquery-3.2.1.js"></script>
<script src="~/js/lib/layui/dist/layui.js"></script>
<link href="~/js/lib/ztree/css/demo.css" rel="stylesheet" />
<link href="~/js/lib/ztree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
<script src="~/js/ztree/jquery-1.4.4.min.js"></script>
<script src="~/js/ztree/jquery.ztree.core-3.5.js"></script>
<script src="~/js/lib/ztree/js/jquery.ztree.excheck-3.5.js"></script>
<script src="~/js/content/tool.js"></script>

<ul id="treeDemo" class="ztree" style="margin-top:0; width:200px; height: 400px; background-color:white"></ul>
<br />
<div style="margin:0 0 0 80%">
    <input onclick="onCheck()" value="保存" class="layui-btn" type="button" />
</div>
<script type="text/javascript">
    var setting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        }
    };
    var roleId = getUrlParam("RoleId");
    var zNodes = [];
    $.ajax({
        type: 'post',
        url: '/Role/GetAllMenuList?RoleId=' + roleId,
        dataType: 'json',
        success: function (data) {
            for (var i = 0, l = data.length; i < l; i++) {
                zNodes.push({ id: data[i].menuId, pId: data[i].menuParentId, name: data[i].menuName, checked: data[i].checked })
            }
            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        }
    });
    function onCheck() {
        
        var treeObj = $.fn.zTree.getZTreeObj("treeDemo"),
            nodes = treeObj.getCheckedNodes(true);
        var MenuId = [];
        var roleId = getUrlParam("RoleId");
        for (var i = 0; i < nodes.length; i++) {
            MenuId.push({ MenuId: nodes[i].id, RoleId: roleId });
        }
        $.ajax({
            type: 'post',
            url: '/Role/SetMenuRoleMsg',
            data: { MenuId: MenuId, RoleId: roleId},
            dataType: 'json',
            success: function (item)
            {
                //if (item.code == 0)  // 设置成功跳转到登录页
                //{ 
                //    window.top.location.href = "../";
                //}
            }
        });
        
        var index = parent.layer.getFrameIndex(window.name); // 得到索引
        parent.layer.close(index);
        parent.alertMsy();
        window.top.location.href = "../";
        
    }
   
</script>